﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using OpenGado.DATA.Entities;
using OpenGado.DATA;

namespace OpenGado.Win
{
    public partial class frmUnitOfMeasure : Form
    {

        public delegate void UpdateUnitOfMeasureHandler();

        public UpdateUnitOfMeasureHandler UpdateUnitList;
        UnitOfMeasure unit;

        public frmUnitOfMeasure()
        {
            InitializeComponent();
        }

        public frmUnitOfMeasure(int unitId)
        {
            InitializeComponent();
            unit = UnitOfMeasureDataControl.Instance.GetById(unitId);

            txtUnit.Text =  unit.Name;

        }
        private void btnSave_Click(object sender, EventArgs e)
        {

            SaveUnit();
            UpdateUnitList();
            this.Close();
            
        }

        private void SaveUnit()
        {
            if (unit != null)
            {
                unit.Name = txtUnit.Text;
                UnitOfMeasureDataControl.Instance.Save(unit);
            }
            else
            {
                UnitOfMeasure u = new UnitOfMeasure();
                u.Name = txtUnit.Text;
                UnitOfMeasureDataControl.Instance.Save(u);
            }
           
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
